The Victory lets us add charts and data visualization into our React app.
In this article, we’ll look at how to add charts into our React app with Victory.
Altering Default Label Components
We can override the appearance of the components that comes with Victory.
For instance, we can change the labels that are displayed with bar charts by writing:
import React from "react";
import { VictoryBar, VictoryLabel } from "victory";
export default function App() {
return (
<VictoryBar
data={[
{ x: 1, y: 3, label: "Alpha" },
{ x: 2, y: 4, label: "Bravo" },
{ x: 3, y: 6, label: "Charlie" },
{ x: 4, y: 3, label: "Delta" },
{ x: 5, y: 7, label: "Echo" }
]}
labelComponent={
<VictoryLabel angle={90} verticalAnchor="middle" textAnchor="end" />
}
/>
);
}
We set the angle
of the VictoryLabel
to display the labels vertically.
textAnchor
is set to end
to move them to the right.
Wrapping Components
We can wrap components to display what we want.
For instance, we can create our own wrapper component to add labels to our chart:
import React from "react";
import { VictoryChart, VictoryLabel, VictoryScatter } from "victory";
const WrapperComponent = (props) => {
const renderChildren = () => {
const children = React.Children.toArray(props.children);
return children.map((child) => {
const style = { ...child.props.style, ...props.style };
return React.cloneElement(
child,
Object.assign({}, child.props, props, { style })
);
});
};
return (
<g transform="translate(20, 40)">
<VictoryLabel text={"add labels"} x={110} y={30} />
<VictoryLabel text={"offset data from axes"} x={70} y={150} />
<VictoryLabel text={"alter props"} x={280} y={150} />
{renderChildren()}
</g>
);
};
export default function App() {
return (
<VictoryChart>
<WrapperComponent>
<VictoryScatter
y={(d) => Math.sin(2 * Math.PI * d.x)}
samples={15}
symbol="square"
size={6}
style={{ data: { stroke: "tomato", strokeWidth: 3 } }}
/>
</WrapperComponent>
</VictoryChart>
);
}
WrapperComponent
takes the child components from the children
prop and render them with React.cloneElement
.
We also combine the styles by merging the styles from the child
and the props.
In the return
statement, we have the VictoryLabel
s and call the renderChildren
function to render the child items.
Then in App
, we render VictoryScatter
in the WrapperComponent
to render the sine curve with the VictoryLabel
s.
Customize Points
For instance, we can create the CatPoint
component and pass that as the value of dataComponent
in the VictoryScatter
component:
import React from "react";
import { VictoryChart, VictoryScatter } from "victory";
const CatPoint = (props) => {
const { x, y, datum } = props;
const cat = datum._y >= 0 ? "?" : "?";
return (
<text x={x} y={y} fontSize={30}>
{cat}
</text>
);
};
export default function App() {
return (
<VictoryChart>
<VictoryScatter
y={(d) => Math.sin(2 * Math.PI * d.x)}
samples={25}
dataComponent={<CatPoint />}
/>
</VictoryChart>
);
}
We get the data entry from the datum
prop, and the _y
property has the y value.
x
and y
has the position.
Conclusion
We can customize labels and points with charts created with Victory in our React app.